package com.ruoyi.api.controller.moon;


import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.moon.domain.*;
import com.ruoyi.moon.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Timer;
import java.util.TimerTask;

@RestController
@RequestMapping("/api/comment")
public class ApiMoonCommentController extends BaseController {

    @Autowired
    private IMoonReviewsService moonReviewsService;
    @Autowired
    private IMoonReplyService moonReplyService;

    @Autowired
    private IMoonUpvoteService moonUpvoteService;

    @Autowired
    private IMoonCompanyService moonCompanyService;

    @Autowired
    private IMoonLeaveMessageService moonLeaveMessageService;


    /**
     * 新增公司
     */
    @RepeatSubmit
    @PostMapping("/addCompany")
    public AjaxResult addCompany(@RequestBody MoonCompany moonCompany) {
        if (StringUtils.isEmpty(moonCompany.getProvince())) {
            return AjaxResult.error("请选择省");
        }
        if (StringUtils.isEmpty(moonCompany.getCity())) {
            return AjaxResult.error("请选择市");
        }
        if (StringUtils.isEmpty(moonCompany.getWage())) {
            return AjaxResult.error("请选择工资发放情况");
        }
        if (StringUtils.isEmpty(moonCompany.getSocialSecurity())) {
            return AjaxResult.error("请选择社保情况");
        }
        if (StringUtils.isEmpty(moonCompany.getOvertimesSituation())) {
            return AjaxResult.error("请选择加班情况");
        }
        if (StringUtils.isEmpty(moonCompany.getReserveFund())) {
            return AjaxResult.error("请选择公积金情况");
        }
        if (StringUtils.isEmpty(moonCompany.getWeekendsClosed())) {
            return AjaxResult.error("请选择休息制度");
        }
        if (StringUtils.isEmpty(moonCompany.getYouWork())) {
            return AjaxResult.error("请选择您的职位");
        }
        if (StringUtils.isEmpty(moonCompany.getDetailedDescription())) {
            return AjaxResult.error("请输入详情描述");
        }
        moonCompany.setUserId(getUserId());
        return toAjax(moonCompanyService.insertCompany(moonCompany));
    }



    /**
     * 发布评论
     */
    @RepeatSubmit
    @PostMapping("/addReviews")
    public AjaxResult add(@RequestBody MoonReviews moonReviews) {
      if (moonReviews.getCompanyRating().equals(0.0)){
          return AjaxResult.error("公司评分不能为空");
        }
        if(StringUtils.isEmpty(moonReviews.getYouWork())){
            return AjaxResult.error("请选择您的职位");
        }
        if(StringUtils.isEmpty(moonReviews.getDetailedDescription())){
            return AjaxResult.error("请输入详情描述");
        }
        moonReviews.setUserId(getUserId());
        moonReviews.setUserName(getUsername());
        return toAjax(moonReviewsService.insertMoonReviews(moonReviews));
    }


    /**
     * 删除评论 小程序端
     */
    @RepeatSubmit
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        boolean success = moonReviewsService.deleteMoonReviews(id, getUserId());
        return toAjax(success);
    }



    /**
     * 新增回复
     */
    @PostMapping("/addReply")
    public AjaxResult add(@RequestBody MoonReply moonReply) {
        moonReply.setUserId(getUserId());
        return toAjax(moonReplyService.insertMoonReply(moonReply));
    }



    /**
     * 新增赞踩管理
     */
    @PostMapping("/addUpvote")
    public AjaxResult add(@RequestBody MoonUpvote moonUpvote) {
        moonUpvote.setUserId(getUserId());
        moonUpvoteService.deleteMoonUpvoteByUserId(moonUpvote.getComReviewsId(),getUserId());
        return toAjax(moonUpvoteService.insertMoonUpvote(moonUpvote));
    }

    /**
     * 取消赞踩
     */
    @RepeatSubmit
    @DeleteMapping("/removeUpvote/{comReviewsId}")
    public AjaxResult removeUpvote(@PathVariable Long comReviewsId)
    {
        if (comReviewsId == null){
            return AjaxResult.error("请联系管理员");
        }
        return toAjax(moonUpvoteService.deleteMoonUpvoteByUserId(comReviewsId,getUserId()));
    }



    /**
     * 新增留言反馈
     */
    @PostMapping("/append")
    public AjaxResult add(@RequestBody MoonLeaveMessage moonLeaveMessage)
    {
        moonLeaveMessage.setMessageIp(IpUtils.getIpAddr());
        moonLeaveMessage.setUserId(getUserId());
        return toAjax(moonLeaveMessageService.insertMoonLeaveMessage(moonLeaveMessage));
    }


}


